package com.sskj.gocoinapp.presenter;

import com.lzy.okgo.OkGo;
import com.lzy.okgo.model.HttpHeaders;
import com.lzy.okgo.model.Response;
import com.sskj.common.base.HttpData;
import com.sskj.common.util.SPUtil;
import com.sskj.common.util.ToastUtil;
import com.sskj.gocoinapp.bean.LoginBean;
import com.sskj.gocoinapp.http.HttpConfig;
import com.sskj.gocoinapp.ui.activity.LoginActivity;
import com.sskj.lib.SPConfig;
import com.sskj.lib.http.JsonCallBack;


public class LoginActivityPresenter extends BasePresenter<LoginActivity> {


    public void login(String mobile, String opwd, boolean isPwd) {
        if (isPwd) {
            login(mobile, opwd);
        } else {
            loginCode(mobile, opwd);
        }
    }

    /**
     * 密码登录
     *
     * @param mobile 手机号/邮箱号
     * @param opwd   密码
     * @return
     */
    public void login(String mobile, String opwd) {

        httpService.login(mobile, opwd).execute(loginCallBack);
    }

    JsonCallBack<HttpData<LoginBean>> loginCallBack = new JsonCallBack<HttpData<LoginBean>>(this) {
        @Override
        public void onSuccess(Response<HttpData<LoginBean>> response) {
            HttpData<LoginBean> httpData = response.body();
            if (httpData.getStatus() == HttpConfig.OK) {

                if (httpData.getData().getIs_start_google() == 0) {
                    mView.loginSuccess(httpData.getData());
                } else {
                    HttpHeaders httpHeaders = new HttpHeaders();
                    httpHeaders.put(SPConfig.ACCOUNT, httpData.getData().getAccount());
                    httpHeaders.put(SPConfig.TOKEN, httpData.getData().getToken());
                    OkGo.getInstance().addCommonHeaders(new HttpHeaders());
                    OkGo.getInstance().getCommonHeaders().clear();
                    OkGo.getInstance().addCommonHeaders(httpHeaders);
                    mView.googleCheck(httpData.getData());
                }
            }
        }
    };

    public void checkGoogle(String code, LoginBean loginBean) {
        httpService.checkGoogle(code)
                .execute(new JsonCallBack<HttpData>(this) {
                    @Override
                    public void onSuccess(Response<HttpData> response) {
                        HttpData httpData = response.body();
                        if (httpData.getStatus() == HttpConfig.OK) {
                            mView.loginSuccess(loginBean);
                        }
                    }
                });
    }

    public void sendCode(String account) {
        if (account.contains("@")) {
            sendEmail(account);
        } else {
            sendSms(account, "3");
        }
    }

    /**
     * 发送验证码（注册）
     *
     * @param mobile 手机号
     * @param type   1注册 （2 重置 3 安全验证 4 资金密码设置 5 提币）
     * @return
     */
    public void sendSms(String mobile, String type) {

        httpService.sendSms(mobile, type).execute(callback);
    }

    JsonCallBack<HttpData> callback = new JsonCallBack<HttpData>(this) {
        @Override
        public void onSuccess(Response<HttpData> response) {
            HttpData httpData = response.body();
            if (httpData.getStatus() == HttpConfig.OK) {
                mView.sendCheckCodeSuccess();
            } else {
                ToastUtil.showShort(httpData.getMsg());
                mView.sendCheckCodeFail();
            }
        }

        @Override
        public void onError(Response<HttpData> response) {
            mView.sendCheckCodeFail();
            super.onError(response);
        }
    };

    /**
     * 发送邮件
     *
     * @param email 邮箱号
     * @return
     */
    public void sendEmail(String email) {
        httpService.sendEmail(email, null).execute(callback);
    }


    /**
     * 验证码登录
     *
     * @param account
     * @param code
     */
    public void loginCode(String account, String code) {

        httpService.loginCode(account, code).execute(loginCallBack);
    }
}
